package com.xhh.nphm.common.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DataConvertUtil {
    /**
     * LocalDateTime转Date
     *
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        if (localDateTime == null) return null;

        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * Date转LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        if (date == null) return null;

        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * LocalDate转Date
     *
     * @param localDate
     * @return
     */
    public static Date localDateToDate(LocalDate localDate) {
        if (localDate == null) return null;

        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /**
     * Date转LocalDate
     *
     * @param date
     * @return
     */
    public static LocalDate dateToLocalDate(Date date) {
        if (date == null) return null;

        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * 日期时间（LocalDateTime）按默认格式转字符串
     *
     * @param value
     * @return
     */
    public static String localDateTimeToStr(LocalDateTime value) {
        return localDateTimeToStr(value, "yyyy-MM-dd");
    }

    /**
     * 日期时间（LocalDateTime）按指定格式转字符串
     *
     * @param value
     * @param format
     * @return
     */
    public static String localDateTimeToStr(LocalDateTime value, String format) {
        String dateString;
        if (value == null) {
            dateString = "";
        } else {
            DateTimeFormatter formatDate = DateTimeFormatter.ofPattern(format);
            dateString = value.format(formatDate);
        }

        return dateString;
    }

    /**
     * 字符串按默认格式转日期时间（LocalDateTime）
     *
     * @param value
     * @return
     */
    public static LocalDateTime strToLocalDateTime(String value) {
        return strToLocalDateTime(value, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 字符串按指定格式转日期时间（LocalDateTime）
     *
     * @param value
     * @param format
     * @return
     */
    public static LocalDateTime strToLocalDateTime(String value, String format) {
        if (value != null && value.trim().length() > 0) {
            DateTimeFormatter formatDate = DateTimeFormatter.ofPattern(format);
            try {
                return LocalDateTime.parse(value, formatDate);
            } catch (Exception e) {
                return null;
            }
        }

        return null;
    }

    public static void main(String[] args) {
        //获取当前时间
        LocalDateTime d0 = LocalDateTime.now();
        System.out.println(DataConvertUtil.localDateTimeToStr(d0, "yyyy-MM-dd HH:mm:ss"));
    }
}
